//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Text;

namespace DNAide.Configuration
{
	public class HttpErrorModuleRuleConfig : ConfigurationElement
	{
		/// <summary>
		/// Http error code to handle.
		/// </summary>
		[ConfigurationProperty("code", DefaultValue = 0, IsRequired = true)]
		public Int32 Code
		{
			get
			{
				return (Int32)this["code"];
			}
			set
			{
				this["code"] = value;
			}
		}

		/// <summary>
		/// Page to handle the error. Can include "~/..." prefix.
		/// </summary>
		[ConfigurationProperty("handlerPage", DefaultValue = null, IsRequired = true)]
		public String HandlerPage
		{
			get
			{
				return (string)this["handlerPage"];
			}
			set
			{
				this["handlerPage"] = value;
			}
		}


	}

	/// <summary>HttpErrorModuleRuleConfigCollection</summary>
	public class HttpErrorModuleRuleConfigCollection : ConfigurationElementCollection, ICollection<HttpErrorModuleRuleConfig>
	{
		/// <summary>IsReadOnly</summary>
		public new bool IsReadOnly
		{
			get
			{
				return base.IsReadOnly();
			}
		}

		/// <summary>HttpErrorModuleRuleConfig</summary>
		public HttpErrorModuleRuleConfig this[int index]
		{
			get
			{
				return (HttpErrorModuleRuleConfig)BaseGet(index);
			}
			set
			{
				if (BaseGet(index) != null)
				{
					BaseRemoveAt(index);
				}
				BaseAdd(index, value);
			}
		}

		/// <summary>Add</summary>
		public void Add(HttpErrorModuleRuleConfig item)
		{
			if (item == null)
				return;

			BaseAdd(item);
		}

		/// <summary>Clear</summary>
		public void Clear()
		{
			BaseClear();
		}

		/// <summary>Contains</summary>
		public bool Contains(HttpErrorModuleRuleConfig item)
		{
			if (BaseIndexOf(item) >= 0)
				return true;
			else
				return false;
		}

		/// <summary>CopyTo</summary>
		public void CopyTo(HttpErrorModuleRuleConfig[] array, int arrayIndex)
		{
			((ICollection)this).CopyTo(array, arrayIndex);
		}

		/// <summary>CreateNewElement</summary>
		protected override ConfigurationElement CreateNewElement()
		{
			return new HttpErrorModuleRuleConfig();
		}

		/// <summary>IndexOf</summary>
		public int IndexOf(HttpErrorModuleRuleConfig item)
		{
			if (item == null)
				return -1;

			return BaseIndexOf(item);
		}

		/// <summary>GetElementKey</summary>
		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((HttpErrorModuleRuleConfig)element).Code;
		}

		/// <summary>GetEnumerator</summary>
		public new IEnumerator<HttpErrorModuleRuleConfig> GetEnumerator()
		{
			for (int i = 0; i < Count; i++)
			{
				yield return this[i];
			}
		}

		/// <summary>Remove</summary>
		public bool Remove(HttpErrorModuleRuleConfig item)
		{
			if (item == null)
				return false;

			int index = BaseIndexOf(item);

			if (index >= 0)
				BaseRemoveAt(index);

			return true;
		}

		/// <summary>RemoveAt</summary>
		public void RemoveAt(int index)
		{
			BaseRemoveAt(index);
		}

	}
}
